package org.hq.web.contryoller;
import org.hq.web.utils.R;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.authentication.AuthenticationManager;
import org.springframework.security.authentication.UsernamePasswordAuthenticationToken;
import org.springframework.security.core.Authentication;
import org.springframework.security.core.context.SecurityContextHolder;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.util.ArrayList;

/**
 * 登录接口实现
 */
@RestController
@RequestMapping
public class Login {
    @Autowired
    private AuthenticationManager am;
    @PostMapping("/logintojson")
    public R login(String username, String password) {
    System.out.println("登录系统用户 "+username);
      try {
          // 封装登录系统用户
          UsernamePasswordAuthenticationToken upa = new UsernamePasswordAuthenticationToken(username, password);
          //认证处理
          Authentication authenticate = am.authenticate(upa);
          //判断是否认证成功
          //将认证用户保存到上下文去
          SecurityContextHolder.getContext().setAuthentication(authenticate);
          // 生成 token
          //保存 token
          return R.ok();
      } catch (Exception e) {

      }

        return R.no();
    }


}
